package cn.ffcs.uec.payroll.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.payroll.action.bean.UecPayRollEditBean;
import cn.ffcs.uec.payroll.model.UecPayRoll;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecPayRollEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecPayRollEditBean bean = new UecPayRollEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 
	 */
	private UecPayRoll uecPayRoll;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * @throws ParseException 
	 */
	public void onCreate$uecPayRollEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {

	}

	/**
	 * bindBean
	 * @throws ParseException 
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		bean.getUecPayRollEditWindow().setWidth("70%");
		bean.getUecPayRollEditExt().getBean().getMonthId().setDisabled(true);
		if ("add".equals(opType)) {
			this.bean.getUecPayRollEditWindow().setTitle("新增发放申请单");
		}
		if ("view".equals(opType)) {
			this.bean.getUecPayRollEditWindow().setTitle("查看发放申请单");
			uecPayRoll = (UecPayRoll) arg.get("uecPayRoll");
			if (null != uecPayRoll) {
				PubUtil.fillBeanFromPo(uecPayRoll, bean.getUecPayRollEditExt().getBean());
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecPayRollEditExt(),true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecPayRollEditWindow().setTitle("编辑发放申请单");
			//bean.getUecPayRollEditDiv().setHeight("40%");
			uecPayRoll = (UecPayRoll) arg.get("uecPayRoll");
			if (null != uecPayRoll) {
				PubUtil.fillBeanFromPo(uecPayRoll, bean.getUecPayRollEditExt().getBean());
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecPayRoll = UecPayRoll.newInstance();
			PubUtil.fillPoFromBean(bean.getUecPayRollEditExt().getBean(), uecPayRoll);
			dateTransToString();
			uecPayRoll.setDr(1L);
			uecPayRoll.addOnly();
			Events.postEvent("onOK", this.self, uecPayRoll);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean.getUecPayRollEditExt().getBean(), uecPayRoll);
			dateTransToString();
			uecPayRoll.updateOnly();
			Events.postEvent("onOK", this.self, uecPayRoll);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecPayRollEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		/* if (bean.getLastflag().getValue().length() > 1) {
			 return "最近记录标识只能一个字符长度！";*/
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourType().getValue())) {
		// return "填写奖励类型";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourMeas().getValue())) {
		// return "填写奖励措施";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourOrg().getValue())) {
		// return "填写奖励机构";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourRank().getValue())) {
		// return "填写奖励级别";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourGrade().getValue())) {
		// return "填写奖励档次";
		// }
		return null;
	}
	/**
	 * 将Date类型时间转成String类型
	 * Date --> String
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getUecPayRollEditExt().getBean().getApplydate().getValue())) {
			uecPayRoll.setApplydate(formatter.format(bean.getUecPayRollEditExt().getBean().getApplydate()
					.getValue()));
		} 
		if (!StrUtil.isNullOrEmpty(bean.getUecPayRollEditExt().getBean().getApprovedate().getValue())) {
			uecPayRoll.setApprovedate(formatter.format(bean.getUecPayRollEditExt().getBean().getApprovedate()
					.getValue()));
		}
	}
	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecPayRoll.getApplydate()) {
			bean.getUecPayRollEditExt().getBean().getApplydate()
					.setValue(sdf.parse(uecPayRoll.getApplydate()));
		}
		if (null != uecPayRoll.getApprovedate()) {
			bean.getUecPayRollEditExt().getBean().getApprovedate()
					.setValue(sdf.parse(uecPayRoll.getApprovedate()));
		}
	}

}
